package com.xs.tool;

import com.alibaba.fastjson.JSON;
import lombok.Builder;
import lombok.experimental.UtilityClass;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

//@Slf4j
@UtilityClass
public class PrintUtil {

    //┏━━━━━ Error. [(DemoAction.java:56).jackson] ━━━ [cmd:1-1 65537] ━━━ [demo游戏逻辑服] ━━━ [roleId:3f93b167-3189-45db-ae1a-0669629a790f]
    //┣ userId: 0
    //┣ 参数: helloReq : HelloReq{name='1'}
    //┣ 响应: [HelloReq{name='data:1'}, HelloReq{name='data:2'}, HelloReq{name='data:3'}, HelloReq{name='data:4'}]
    //┣ 错误码: 100
    //┣ 错误信息: 异常机制测试，name 必须是 jackson !
    //┣ 耗时: 2 ms
    //┗━━━━━ [ioGame:21.23] ━━━━━ [线程:User-16-15] ━━━━━ [连接方式:WebSocket] ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

    private static final String format =
                    "┏━━━━━ [%s] [%s] [(%s).%s] [cmd:%s] [%s]%n" +
                    "┣ 消息Id: %s%n" +
                    "┣ 参数: %s%n" +
                    "┣ 响应: %s%n" +
                    "┣ 标识码: %d%n" +
                    "┣ 错误信息: %s%n" +
                    "┣ 耗时: %s ms 消息大小:%sKb %n" +
                    "┗━━━━━ [%s] ━━━━━ [线程:%s] ━━━━━ [连接方式:%s] ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━";
    private static final Logger log = LoggerFactory.getLogger(PrintUtil.class);

    @Builder
    public static class PrintMessage{
        /// 日志级别
        public String level;
        /// time
        public  String time;
        /// ClassName
        public  String className;
        /// methodName
        public  String methodName;
        /// cmd
        public  String cmd;
        /// 逻辑服务名称
        public  String logicServerName;
        /// roleId
        public  String id;
        /// userId
        public  String userId;
        /// 参数
        public  String param;
        /// 响应
        public  Object response;
        /// 异常
        public Exception e;
        /// 标识码
        public  int code;
        /// 错误信息
        public  String errorMsg;
        /// 耗时
        public  Long cost;
        /// 大小
        public  double size = 0;
        /// 应用:版本号
        public  String appVersion;
        /// 线程
        public  String threadName;
        /// 连接方式
        public  String connectType;
    }

    private static final DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
    public static void print(PrintMessage printMessage){
//        printMessage.time = formatter.format(LocalDateTime.now());
//        // lv 判定 @todo 改为code判定
//        switch (printMessage.level){
//            case "info","Info":
//                System.out.print("\033[32m");
//                break;
//            case "warn","Warn":
//                System.out.print("\033[33m");
//                break;
//            case "error","Error":
//                System.out.print("\033[31m");
//                break;
//            default:
//                System.out.print("\033[0m");
//        }
//
//        System.out.printf(format,
//                printMessage.level,
//                printMessage.time,
//                printMessage.className,
//                printMessage.methodName,
//                printMessage.cmd,
//                printMessage.logicServerName,
//                printMessage.userId,
//                printMessage.param,
//                JSON.toJSONString(printMessage.response),
//                printMessage.code,
//                printMessage.errorMsg,
//                printMessage.cost,
//                printMessage.size,
//                printMessage.appVersion,
//                printMessage.threadName,
//                printMessage.connectType
//        );
//        if (printMessage.code>200){
//            log.error("e" , printMessage.e);
//        }
//
//        // 重置颜色
//        System.out.println("\033[0m");
    }
}
